package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.mapper.DeptMapper;
import com.qk.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    // 补全基础属性
    @Override
    public void add(Dept dept) {

        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());

        //调用Mapper
        deptMapper.insert(dept);

    }

    @Override
    public PageResult<Dept> list(String name, Integer status, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        List<Dept> deptList = deptMapper.selectList(name,status);

        Page<Dept> p = (Page<Dept>) deptList;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    public Dept getById(Integer id) {
        return deptMapper.selectByid(id);
    }

    @Override
    public void update(Dept dept) {
        //1.补全属性  updateTime
        dept.setUpdateTime(LocalDateTime.now());
        //2.调用Mapper接口方法
        deptMapper.update(dept);
    }

    @Override
    public void delete(Integer id) {
        deptMapper.delete(id);
    }

    @Override
    public List< Dept> findList() {
         return deptMapper.listAll();
    }
}